﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RelayCommand.cs" company="Mar3ek">
//   http://mar3ek.wordpress.com
// </copyright>
// <summary>
//   Defines the RelayCommand type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Mar3ek.HouseLight
{
    using System;
    using System.Windows.Input;

    /// <summary>
    /// Simple delegate command
    /// </summary>
    public class RelayCommand : ICommand
    {
        /// <summary>
        /// Action method
        /// </summary>
        private readonly RelayActionDelegate _action;

        /// <summary>
        /// Can execute method
        /// </summary>
        private readonly RelayCanExecuteDelegate _canExecute;

        /// <summary>
        /// Temporary can execute value
        /// </summary>
        private bool _tempCanExecute;

        /// <summary>
        /// Initializes a new instance of the <see cref="RelayCommand"/> class.
        /// </summary>
        /// <param name="action">The action.</param>
        /// <param name="canExecute">The can execute.</param>
        public RelayCommand(RelayActionDelegate action, RelayCanExecuteDelegate canExecute)
        {
            _action = action;
            _canExecute = canExecute;
        }

        /// <summary>
        /// Delegate for the command action
        /// </summary>
        /// <param name="parameter">
        /// The command parameter.
        /// </param>
        public delegate void RelayActionDelegate(object parameter);

        /// <summary>
        /// Delegate for the CanExecute method
        /// </summary>
        /// <param name="parameter">The command parameter.</param>
        /// <returns>True if CanExecute is truel; otherwise false.</returns>
        public delegate bool RelayCanExecuteDelegate(object parameter);

        /// <summary>
        /// Occurs when changes occur that affect whether or not the command should execute.
        /// </summary>
        public event EventHandler CanExecuteChanged;

        /// <summary>
        /// Defines the method to be called when the command is invoked.
        /// </summary>
        /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
        public void Execute(object parameter)
        {
            _action.Invoke(parameter);
        }

        /// <summary>
        /// Defines the method that determines whether the command can execute in its current state.
        /// </summary>
        /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
        /// <returns>
        /// true if this command can be executed; otherwise, false.
        /// </returns>
        public bool CanExecute(object parameter)
        {
            var canExecute = _canExecute.Invoke(parameter);
            if (canExecute != _tempCanExecute)
            {
                _tempCanExecute = canExecute;
                if (CanExecuteChanged != null)
                {
                    CanExecuteChanged(this, new EventArgs());
                }
            }

            return _tempCanExecute;
        }
    }
}